requirejs.config({
    baseUrl: '../js/libs',
    paths: {
        ztree:'../plugins/ztree/js/jquery.ztree.core',
        ztreeCheck:'../plugins/ztree/js/jquery.ztree.excheck.min',
        ztreeHide:'../plugins/ztree/js/jquery.ztree.exhide'
    },
    waitSeconds : 15,
	map : {
		'*' : {
			'css' : 'css'
		}
	},
    shim: { //引入没有使用requirejs模块写法的类库。backbone依赖underscore
        'ztreeCheck':['css!../plugins/ztree/css/zTreeStyle/zTreeStyle.css'],
        'ztreeHide':['ztree']
    }
});
define([ 'jquery', 'jquery_route', 'arttemplateUtil' ,'common','config','pageJs','validform','ztree','ztreeCheck','ztreeHide','wdatePicker' ], function($,
		jquery_route, arttemplateUtil,common,config,pageJs  ) {
	
	$.controller("controller.authority.logs", function($model, $tplHtml, $defobj,
			$modelobj, $paramobj, $this) {
		//渲染到页面方法
		$model($defobj);	
		var param={};
		var initLogsData = function($log_type){
			common.loading();
			if(typeof $log_type != 'undefined'){
				param.list_tab=$log_type;
			}else{
				param.list_tab=$('#tabs_logs li.active a').attr('tab_value');
			}
			
			param.first_operate_date=$('#first_operate_date').val();
			param.last_operate_date=$('#last_operate_date').val();
			param.module_type=$('#module_type').val();
			param.role_name=$('#role_name').val();
			param.user_account=$('#user_account').val();
			
			common.ajax_request2('post','/api/seller/v1/queryLog',param,function($data,$code){
				$('#logs_list .tab-content .tab-pane').empty();
				$defobj.data = $data;
				if(typeof $defobj.data != 'undefined'){
					pageJs.setPage($defobj.data.pageNo,$defobj.data.totalPage,$defobj.data.totalCount,callback);
				}
				$('#logs_list #tab'+param.list_tab).html(arttemplateUtil.template.compile($.setHTML('authority/templates/logs_tpl.html'))($defobj));
				layer.closeAll('loading'); //关闭加载层
			});
		}
		
		var _logsFrm=$('#logsBody #serchForm');
		// 搜索
		$('#logsBody #serchForm .serchBtn').off('click').on('click',function(){
			param.pageNo=1;
			initLogsData();
		});
		// 重置
		$('#logsBody #serchForm .resetBtn').off('click').on('click',function(){
			if(_logsFrm.length>0){
				_logsFrm[0].reset();
			}
			param.pageNo=1;
			initLogsData();
		});
		//选中标签
		$('#logsBody #logs_list li').off('click').on('click',function(){
			param.pageNo = 1;
			initLogsData($(this).find('a').attr('tab_value'));
		});
		
		//分页回调
		var callback = function(page){
			param.pageNo=page
			initLogsData();
		}
		initLogsData();
		
	});
	$.controller("controller.authority.auth", function($model, $tplHtml, $defobj,
			$modelobj, $paramobj, $this) {
		//渲染到页面方法
		$model($defobj);
		
		var setting = {
				data: {
					key: {
						url:null,
						name:"roleName"
					},
					simpleData: {
						enable: true,
						idKey: "id",
						pIdKey: "pid",
						rootPId: config.sysconfig().roleparentid
					}
				},
				callback: {
					onRightClick: zTreeOnRightClick,
					onClick: zTreeOnClick
				}
			};
		function zTreeOnClick(event, treeId, treeNode) {
			$('#parentid').val(treeNode.pid);
			if(treeNode.getParentNode() == null){
				$('#parentname').val('顶级');
			}else{
				$('#parentname').val(treeNode.getParentNode().roleName);
			}
			$('#roleid').val(treeNode.id);
			$('#rolename').val(treeNode.roleName);
			$('#roleurl').val(treeNode.url);
			if(treeNode.sequence=='0'){
				$('#roleseq').val('100');
			}else{
				$('#roleseq').val(treeNode.sequence);
			}
			$('#display1').removeAttr('checked');
			$('#display0').removeAttr('checked');
			if(treeNode.display){
				$('#display1').attr('checked',true);
			}else{
				$('#display0').attr('checked',true);
			}
			$('#rolefrm input[type=button]').show();
		};
		function zTreeOnRightClick(event, treeId, treeNode) {
			if(confirm("父级确定改为:"+treeNode.roleName)){
				$('#parentid').val(treeNode.id);
				$('#parentname').val(treeNode.roleName);
				$('#rolefrm input[type=button]').show();
			}
		};
		var zNodes =[
		 			{ idx:1, pId:0, name:"可折腾的父节点 1", t:"我很普通，随便 展开/折叠 我吧", open:false},
		 			{ idx:11, pId:1, name:"叶子节点 - 1", t:"我老爸很普通，随便折腾他吧"},
		 			{ idx:12, pId:1, name:"可折腾的父节点 2", t:"我和我老爸都很普通，随便折腾我和他吧", open: false},
		 			{ idx:121, pId:12, name:"父节点 - 121", t:"我老爸很普通，随便折腾他吧", open:true},
		 			{ idx:1211, pId:121, name:"父节点 - 1211", t:"我老爸很普通，随便折腾他吧", open:false},
		 			{ idx:1212, pId:1211, name:"叶子节点 - 1212", t:"我老爸很普通，随便折腾他吧"},
		 			{ idx:1213, pId:121, name:"父节点 - 1213", t:"我老爸很普通，随便折腾他吧", open:false},
		 			{ idx:1214, pId:1213, name:"叶子节点 - 1214", t:"我老爸很普通，随便折腾他吧"},
		 			{ idx:123, pId:12, name:"叶子节点 - 23", t:"我老爸很普通，随便折腾他吧"},
		 			{ idx:13, pId:1, name:"叶子节点 - 3", t:"我老爸很普通，随便折腾他吧"},
		 			{ idx:2, pId:1, name:"无法折叠的父节点", t:"休想让我折叠起来...除非你用 expandAll 方法", open:false, collapse:false},
		 			{ idx:21, pId:2, name:"叶子节点2 - 1", t:"哈哈哈，我老爸NB吧，不能折叠吧？"},
		 			{ idx:22, pId:21, name:"叶子节点2 - 2", t:"哈哈哈，我老爸NB吧，不能折叠吧？"},
		 			{ idx:23, pId:21, name:"叶子节点2 - 3", t:"哈哈哈，我老爸NB吧，不能折叠吧？"},
		 			{ idx:3, pId:2, name:"无法展开的父节点", t:"就凭你也想展开我？难呀...嘿嘿, 除非你用 expandAll 方法", open:false, expand:true},
		 			{ idx:31, pId:3, name:"叶子节点3 - 1", t:"居然让你看到了...莫非你用了全部节点展开？"},
		 			{ idx:32, pId:31, name:"叶子节点3 - 2", t:"居然让你看到了...莫非你用了全部节点展开？"},
		 			{ idx:33, pId:31, name:"叶子节点3 - 3", t:"居然让你看到了...莫非你用了全部节点展开？"},
		 			{ idx:4, pId:3, name:"空空的父节点 1", t:"一无所有...除了我自己", isParent:true, open:false}
		 		];
		
		var loadztree=function(){
			$('#rolefrm input[type=button]').hide();
			common.ajax_request2('post','/api/seller/v1/queryRoleList',{},function($data,$code){
				zNodes=$data;
			},false);
			var _ztree=$.fn.zTree.init($("#treeDemo"), setting, zNodes);
			_ztree.expandAll(true);
		};
		loadztree();
		$('#rolefrm').off('click','input[type=button]').on('click','input[type=button]',function(){
			var datatype=$(this).attr('data-type');
			if(datatype=='rolefefresh'){
				$('#rolefrm')[0].reset();
				loadztree();
			}else{
				$('#datatype').val(datatype);
				if('roledel'==datatype&&!confirm('确定删除:'+$('#rolename').val())){
					return;
				}else if('roleedit'==datatype&&!confirm('确定修改:'+$('#rolename').val())){
					return;
				}
				common.ajax_request2('post','/api/seller/v1/updateRole',$('#rolefrm').serializeArray(),function($data,$code,$business){
					if($code==200){
						$('#rolefrm')[0].reset();
						window.layer.msg("操作成功!");
						if($('#isreload').is(':checked')){
							loadztree();
						}
					}else{
						window.layer.msg($business.businessNote);
					}
					layer.closeAll('loading'); //关闭加载层
				});
			}
		});
		
		//鼠标经过移开
		$(document).off('mouseenter','.logbox .memolink').on('mouseenter','.logbox .memolink',function(){
			$(this).popover('show');
		});
		$(document).off('mouseleave','.logbox .memolink').on('mouseleave','.logbox .memolink',function(){
			$(this).popover('hide');
		});
		
		$('#setrolefrm').off('click','input[type=button]').on('click','input[type=button]',function(){
			var datatype=$(this).attr('data-type');
			if(datatype=='setrole'){
				common.loading();
				common.ajax_request2('post','/api/v1/setrolejson',$('#setrolefrm').serializeArray(),function($data,$code,$business){
					if($code==200){
						$('#rolefrm')[0].reset();
						window.layer.msg("操作成功!");
						if($('#isreload').is(':checked')){
							loadztree();
						}
					}else{
						window.layer.msg($business.businessNote);
					}
					layer.closeAll('loading'); //关闭加载层
				});
			}else if(datatype=='getrole'){
				common.loading();
				common.ajax_request2('post','/api/v1/getrolejson',{},function($data,$code,$business){
					if($code==200){
						$('#rolefrm')[0].reset();
						$('#setrolefrm textarea').val(JSON.stringify($data));
					}else{
						window.layer.msg($business.businessNote);
					}
					layer.closeAll('loading'); //关闭加载层
				});
			}
		});
	});
	var App={
			name:'权限管理'
	};
});